package com.xyht.sca_s.student_manage_system.modules.scheduleManagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.util.CacheUtil;
import com.xyht.sca_s.student_manage_system.modules.scheduleManagement.entity.SmsScheduleSubscribe;
import com.xyht.sca_s.student_manage_system.modules.scheduleManagement.entity.req.SmsScheduleSubscribeReq;
import com.xyht.sca_s.student_manage_system.modules.scheduleManagement.entity.resp.SmsScheduleSubscribeResp;
import com.xyht.sca_s.student_manage_system.modules.scheduleManagement.entity.resp.SmsUserInfoResp;
import com.xyht.sca_s.student_manage_system.modules.scheduleManagement.mapper.SmsScheduleSubscribeMapper;
import com.xyht.sca_s.student_manage_system.modules.scheduleManagement.service.SmsScheduleSubscribeService;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;

/**
 * <p>
 * 日程订阅表 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-08-14
 */
@Service
public class SmsScheduleSubscribeServiceImpl extends ServiceImpl<SmsScheduleSubscribeMapper, SmsScheduleSubscribe> implements SmsScheduleSubscribeService {
    @Autowired
    private SmsScheduleSubscribeMapper smsScheduleSubscribeMapper;
    @Autowired
    private CacheUtil cacheUtil;

    @Override
    public ResponseResult addSubscribe(SmsScheduleSubscribeReq subscribeReq, String user_id) {
        if (isNullOrEmpty(subscribeReq.getSubscribeUserId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        List<SmsScheduleSubscribe> list = smsScheduleSubscribeMapper.selectList(new LambdaQueryWrapper<SmsScheduleSubscribe>()
                .eq(SmsScheduleSubscribe::getCreatorId, user_id)
                .eq(SmsScheduleSubscribe::getSubscribeUserId, subscribeReq.getSubscribeUserId()));
        if (list != null && list.size() > 0) {
            return CommonResult.failed(CommonCodeEnum.SCHEDULE_SUBSCRIBE_USER_REPEAT);
        }

        SmsScheduleSubscribe addSubscribe = new SmsScheduleSubscribe();
        addSubscribe.setSubscribeColor(subscribeReq.getSubscribeColor());
        addSubscribe.setSubscribeUserId(subscribeReq.getSubscribeUserId());
        addSubscribe.setCreatorId(user_id);
        smsScheduleSubscribeMapper.insert(addSubscribe);
        return CommonResult.success();
    }

    @Override
    public ResponseResult getSubscribeList(String user_id) {
        List<SmsScheduleSubscribeResp> list = smsScheduleSubscribeMapper.selectList(new LambdaQueryWrapper<SmsScheduleSubscribe>()
                .eq(SmsScheduleSubscribe::getCreatorId,user_id))
                .stream()
                .map(subscribe->{
                    SmsScheduleSubscribeResp smsScheduleSubscribeResp = new SmsScheduleSubscribeResp();
                    BeanUtils.copyProperties(subscribe,smsScheduleSubscribeResp);

                    SmsUser user = cacheUtil.getUserInfo(subscribe.getSubscribeUserId());
                    if (user!=null){
                        SmsUserInfoResp smsUserInfoResp = new SmsUserInfoResp();
                        BeanUtils.copyProperties(user,smsUserInfoResp);
                        smsScheduleSubscribeResp.setUserInfo(smsUserInfoResp);
                    }
                    return smsScheduleSubscribeResp;
                })
                .collect(Collectors.toList());
        return CommonResult.success(list);
    }

    @Override
    public ResponseResult updateSubscribeList(SmsScheduleSubscribeReq subscribeReq, String user_id) {
        if (isNullOrEmpty(subscribeReq.getId()) || isNullOrEmpty(subscribeReq.getSubscribeColor())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        SmsScheduleSubscribe subscribe = smsScheduleSubscribeMapper.selectById(subscribeReq.getId());
        if (subscribe == null){
            return CommonResult.failed(CommonCodeEnum.SCHEDULE_SUBSCRIBE_NOT_EXIST);
        }

        SmsScheduleSubscribe updateSubscribe = new SmsScheduleSubscribe();
        updateSubscribe.setId(subscribeReq.getId());
        updateSubscribe.setSubscribeColor(subscribeReq.getSubscribeColor());
        smsScheduleSubscribeMapper.updateById(updateSubscribe);
        return CommonResult.success();
    }

    @Override
    public ResponseResult cancelSubscribeList(SmsScheduleSubscribeReq subscribeReq, String user_id) {
        if (isNullOrEmpty(subscribeReq.getId())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        SmsScheduleSubscribe subscribe = smsScheduleSubscribeMapper.selectById(subscribeReq.getId());
        if (subscribe == null){
            return CommonResult.failed(CommonCodeEnum.SCHEDULE_SUBSCRIBE_NOT_EXIST);
        }

        smsScheduleSubscribeMapper.deleteById(subscribeReq.getId());
        return CommonResult.success();
    }
}
